<%@ page import="java.util.Enumeration"%>
<%!
// java.io.*,java.net.*
// 使用GET方式向目的服务器发送请求
public static String HTTPRequest(String url, HashMap<String, String> cookie) throws IOException{
    String result = "", line;
	// 拼凑get请求的URL字串，使用URLEncoder.encode对特殊和不可见字符进行编码
	// String getURL = GET_URL + "&activatecode=" + URLEncoder.encode(url);
    URL getUrl = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
    connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
    connection.setRequestProperty("Host", getUrl.getHost());

    if(cookie != null){ // 设置请求的 Cookie 写死了
    	String cookieStr = "";
	    for (String key : cookie.keySet())cookieStr += key + "=" + cookie.get(key) + ";";
		
		cookieStr = cookieStr.substring(0, cookieStr.length()-1);
    	connection.setRequestProperty("Cookie", cookieStr);
	}

    connection.connect();

    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));

    while ((line = reader.readLine()) != null){
    	result += line;
    	// lines = new String(line.getBytes(), "utf-8");
        // System.out.println(line);
    }

    reader.close();
    connection.disconnect();// 断开连接

    return result;		
}
public static String HTTPRequest(String url) throws IOException{
	return HTTPRequest(url, null);
}

// 使用 POST 方式向目的服务器发送请求
public static String HTTPPost(String url, String params, HashMap<String, String> cookie) throws IOException{
	String result = "", line;
	
	URL connect = new URL(url);
	HttpURLConnection connection = (HttpURLConnection)connect.openConnection();
	connection.setConnectTimeout(3000);  
	connection.setReadTimeout(3000);  
	connection.setDoOutput(true);
	connection.setDoInput(true);
	connection.setUseCaches(false);  
	connection.setRequestMethod("POST");
	connection.setRequestProperty("Content-type","application/x-www-form-urlencoded;charset=utf-8");

    connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
    connection.setRequestProperty("Host", connect.getHost());

    if(cookie != null){ // 设置请求的 Cookie 写死了
    	String cookieStr = "";
	    for (String key : cookie.keySet())cookieStr += key + "=" + cookie.get(key) + ";";
		
		cookieStr = cookieStr.substring(0, cookieStr.length()-1);
    	connection.setRequestProperty("Cookie", cookieStr);
	}

	connection.connect();

	//System.out.println("url:" + url); 
	//System.out.println("para:" + params); 
 
 	OutputStream os = connection.getOutputStream();  
    os.write(params.getBytes()); 
	os.flush();
	os.close();

    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));

	while((line = reader.readLine()) != null){
		result += line;
		// out.println(line);
	}
	reader.close();
	System.out.println("result:" + result);

	return result;
}

public static String[] getPOST_Data() throws UnsupportedEncodingException{
	String url = null, param;
	StringBuffer params = new StringBuffer();
	Enumeration enu = req.getParameterNames();
	int total = 0;

	while (enu.hasMoreElements()) {
		String paramName = (String)enu.nextElement();
		if(paramName.equals("url")){
			url = req.getParameter(paramName);
		}else{
			String pair = URLEncoder.encode(req.getParameter(paramName), "UTF-8");
			//String pair = req.getParameter(paramName);
			
			if(total == 0)params.append(paramName).append("=").append(pair);
			else params.append("&").append(paramName).append("=").append(pair);
			
			// System.out.println("paramName:" + paramName + "/" + req.getParameter(paramName)); 
			++total;
		}
	}
	param = params.toString();

	String [] arr = {url, param};
	return arr;
}
// 获取请求参数， 左手转右手
public static String getPOST_Data(Boolean isOutputAsString) throws UnsupportedEncodingException{
	String[] arr = getPOST_Data();
	
	String url = ""; int i = 0;
	for(String s : arr){
		if(i==0)url += "?";

		i++;
		url += "&"+s;
	}
	return url;
}
%>

<%!
// 获取 ip 地址
public static String getClientIP(HttpServletRequest httpservletrequest) {
    if (httpservletrequest == null)
        return null;
    String s = httpservletrequest.getHeader("X-Forwarded-For");
    if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s))
        s = httpservletrequest.getHeader("Proxy-Client-IP");
    if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s))
        s = httpservletrequest.getHeader("WL-Proxy-Client-IP");
    if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s))
        s = httpservletrequest.getHeader("HTTP_CLIENT_IP");
    if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s))
        s = httpservletrequest.getHeader("HTTP_X_FORWARDED_FOR");
    if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s))
        s = httpservletrequest.getRemoteAddr();
    if ("127.0.0.1".equals(s) || "0:0:0:0:0:0:0:1".equals(s))
        try {
            s = InetAddress.getLocalHost().getHostAddress();
        }
        catch (UnknownHostException unknownhostexception) {
        }
    return s;
}
%>
